﻿<UserControl x:Class="PrestoDashboard.Tabs.GlobalVariableView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="700">
    <UserControl.DataContext>
        <Binding Source="{StaticResource CustomVariableGroupViewModel}"/>
    </UserControl.DataContext>

    <!-- This works for the initial sort, but when members get added to the collection
         the sort doesn't get updated. That's because CollectionViewSource doesn't 
         implement INotifyPropertyChanged. -->
    <UserControl.Resources>
        
        <CollectionViewSource x:Key="SortedGroups" Source="{Binding CustomVariableGroups}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name" Direction="Ascending"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <DockPanel Grid.Column="0" LastChildFill="True">
            <Label DockPanel.Dock="Top" Style="{StaticResource StandardLabel}" Content="Custom Variable Groups" HorizontalAlignment="Center" FontWeight="Bold"/>
            <StackPanel DockPanel.Dock="Bottom" Orientation="Vertical" Height="80" HorizontalAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource StandardButton}" Content="Add..." Command="{Binding AddVariableGroupCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Refresh" Command="{Binding RefreshVariableGroupCommand}" />
                    <!--<Button Style="{StaticResource StandardButton}" Content="Delete..." Command="{Binding DeleteVariableGroupCommand}" />-->
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource StandardButton}" Content="Import..." Command="{Binding ImportVariableGroupCommand}" />
                    <Button Style="{StaticResource StandardButton}" Content="Export..." Command="{Binding ExportVariableGroupCommand}" />
                </StackPanel>
            </StackPanel>
                <ListBox DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource SortedGroups}}"
                         DisplayMemberPath="Name"
                         SelectedItem="{Binding SelectedCustomVariableGroup}" Height="auto"/>
        </DockPanel>

        <DockPanel Grid.Column="1" LastChildFill="True" IsEnabled="{Binding CustomVariableGroupIsSelected}">
            
            <StackPanel Orientation="Vertical" DockPanel.Dock="Top">

                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="25"/>
                        <RowDefinition Height="25"/>
                        <RowDefinition Height="40"/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="140"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource StandardLabel}" Content="Selected Group" HorizontalAlignment="Center" FontWeight="Bold"/>

                    <Label Grid.Column="0" Grid.Row="1" Style="{StaticResource StandardLabel}" Content="Name: "/>
                    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SelectedCustomVariableGroup.Name}"/>

                    <Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Style="{StaticResource StandardButton}" Content="Save" Command="{Binding SaveVariableGroupCommand}" />

                </Grid>
                
            </StackPanel>

            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center">
                <Button Style="{StaticResource StandardButton}" Content="Add..." Command="{Binding AddVariableCommand}" />
                <Button Style="{StaticResource StandardButton}" Content="Edit..." Command="{Binding EditVariableCommand}" />
                <Button Style="{StaticResource StandardButton}" Content="Delete..." Command="{Binding DeleteVariableCommand}" />                
            </StackPanel>

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Label Grid.Row="0" Style="{StaticResource StandardLabel}" Content="Custom Variables" FontWeight="Bold"/>            
                <DataGrid Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column"
                      ItemsSource="{Binding SelectedCustomVariableGroup.CustomVariables}"
                      SelectedItem="{Binding SelectedCustomVariable}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Path=Key}" Header="Key" Width="170"/>
                        <DataGridTextColumn Binding="{Binding Path=Value}" Header="Value" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
                
        </DockPanel>

    </Grid>
</UserControl>
